<?php

/**
 * 评价列表
 */

namespace app\shop\api;

use \app\base\api\BaseApi;

class CommentApi extends BaseApi {

    /**
     * 列表
     */
    public function index() {
        $id = $this->data['id'];
        $app = $this->data['app'];
        if(empty($id) || empty($app)){
            $this->error404();
        }
        $info = target($app . '/' . $app)->getInfo($id);
        if(empty($info) || !$info['status']) {
            $this->error404();
        }

        $info['images'] = unserialize($info['images']);

        $type = $this->data['type'];

        $where = [];
        switch($type) {
            case 1:
                $where['_sql'] = 'A.level == 4 OR A.level == 5';
                break;
            case 2:
                $where['level'] = 'A.level == 2 OR A.level == 3';
                break;
            case 3:
                $where['level'] = 'A.level == 0 OR A.level == 1';
                break;
        }
        $where['A.app'] = $app;
        $where['A.has_id'] = $id;

        $pageLimit = $this->data['limit'] ? $this->data['limit'] : 10;

        $model = target('order/OrderComment');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit);
        $list = $model->loadList($where, $pageData['limit'], 'time desc');

        $commentStatis = target('shop/Shop', 'service')->getCommentStatis('mall', $id);

        if ($list) {
            $this->success('ok', [
                'data' => $list,
                'pageData' => [
                    'commentStatis' => $commentStatis,
                    'pageLimit' => $pageLimit,
                    'limit' => count($list),
                    'page' => $pageData['page'],
                    'totalPage' => $pageData['totalPage']
                ]
            ]);
        } else {
            $this->error('暂无更多评论', 404);
        }

    }

}